// 📦 工具与依赖
const _ = require("lodash");
const path = require("path");
const fs = require("fs");
const esm = require("esm");
const requireModule = esm(module);

// 📦 项目模块
const { readAllValues } = requireModule("../../../awakened-potential/helpers/readAllValues.js");
const checkAccuracy = requireModule("../../utils/checkAccuracy.js");
const select32NumbersByS1 = require("./select32NumbersByS1.js");

// ✅ 读取历史数据
const allValues = readAllValues("m");

// ✅ 窗口参数
const useWindow = true;
const windowSize = 39;

// ✅ 记录预测结果
const predictResults = [];

// ✅ 遍历每期进行预测
allValues.forEach((item, index) => {
  const temp = useWindow
    ? allValues.slice(Math.max(0, index - windowSize), index)
    : allValues.slice(0, index);

  if (!temp.length) return;

  // ✅ 使用封装后的 S1 策略
  const predictedNumbers = select32NumbersByS1(temp);

  // ✅ 命中判断
  predictResults.push(checkAccuracy(predictedNumbers, item.number));
});

// ✅ 准确率统计
const total = predictResults.length;
const correct = predictResults.filter((x) => x === 1).length;
const accuracy = total > 0 ? (correct / total * 100).toFixed(2) : 0;

// ✅ 连错统计
let maxWrongStreak = 0;
let currentStreak = 0;
predictResults.forEach((res) => {
  if (res === 0) {
    currentStreak++;
    if (currentStreak > maxWrongStreak) {
      maxWrongStreak = currentStreak;
    }
  } else {
    currentStreak = 0;
  }
});

// ✅ 输出统计结果
console.log(`预测总数: ${total}`);
console.log(`命中次数: ${correct}`);
console.log(`准确率: ${accuracy}%`);
console.log(`最大连续错误次数: ${maxWrongStreak}`);
